import { _decorator, Button, Component, find, Label, Node, Sprite, SpriteFrame } from 'cc';
import { HeroData } from '../Data';
import { NodeUtil } from '../AshFramework/Utils/NodeUtil';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import Banner from '../Banner';
import { GameManager } from '../GameManager';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import UI_BreatheAni from '../AshFramework/UI/UI_BreatheAni';
import { StartPanel } from './StartPanel';

const { ccclass, property } = _decorator;

@ccclass('HeroItem')
export class HeroItem extends Component {
    Icon: Sprite = null;
    BG: Node;
    Locked: Node = null;
    FreeGetButton: Node;
    BuyButton: Node = null;
    BuyButtonLabel: Label = null;
    InUseLabel: Node = null;

    UI_BreatheAni: UI_BreatheAni;

    data: HeroData = null;
    cb: Function = null;

    onLoad(): void {
        this.Locked = find("Locked", this.node);
        this.Icon = find("Icon", this.node).getComponent(Sprite);
        this.BG = NodeUtil.GetNode("BG", this.node);
        this.FreeGetButton = NodeUtil.GetNode("FreeGetButton", this.node);
        this.InUseLabel = NodeUtil.GetNode("InUseLabel", this.node);
        this.BuyButton = NodeUtil.GetNode("BuyButton", this.node);
        this.BuyButtonLabel = NodeUtil.GetComponent("BuyButtonLabel", this.node, Label);
        this.UI_BreatheAni = this.node.getComponent(UI_BreatheAni);
    }

    //普通卡牌
    Init(data: HeroData) {
        this.Reset();
        this.data = data;

        this.BG.children.forEach(e => {
            e.active = e.name == `${data.Quality}`;
            if (e.name == `${data.Quality}`) e.getComponent(Sprite).grayscale = !data.IsUnlock;
        });

        this.Refresh();

        ResourceUtil.LoadSpriteFrame(`Heroes/${data.Index}`).then((sp: SpriteFrame) => this.Icon.spriteFrame = sp);
    }

    //点击卡牌有特殊回调
    InitWithCallback(data: HeroData, cb: Function) {
        this.Init(data);
        this.cb = cb;
    }

    Refresh() {
        this.Icon.grayscale = !this.data.IsUnlock;
        this.Locked.active = !this.data.IsUnlock;
        this.FreeGetButton.active = !this.data.IsUnlock;
        this.BuyButton.active = !this.data.IsUnlock;
        this.InUseLabel.active = this.data.IsUnlock && this.data.IsSelected;

        if (!this.data.IsUnlock) {
            this.BuyButtonLabel.string = `${this.data.Price}`;
        }

        this.BG.children.forEach(e => {
            if (e.name == `${this.data.Quality}`) e.getComponent(Sprite).grayscale = !this.data.IsUnlock;
        })

        if (this.data.IsSelected) this.UI_BreatheAni.Set(); else this.UI_BreatheAni.Stop();
    }

    Reset() {
        this.cb = null;
        this.node.getComponent(Button).enabled = true;
    }

    OnButtonClick() {
        AudioManager.PlaySound(Audios.ButtonClick);

        if (this.cb) {
            this.cb(this.data);
            return;
        }

        if (this.data.IsUnlock && this.data.IsSelected) return;

        if (this.data.IsUnlock) {
            GameManager.Instance.SelectHero(this.data.Index);
            StartPanel.Instance.RefreshCharacter();
            EventManager.Scene.emit(MyEvent.REFRESH_SHOPITEMS);
        } else {
            Banner.Instance.ShowVideoAd(() => {
                this.data.IsUnlock = true;
                GameManager.Instance.SelectHero(this.data.Index);
                StartPanel.Instance.RefreshCharacter();
                EventManager.Scene.emit(MyEvent.REFRESH_SHOPITEMS);
            });
        }

    }
}